
/* Generated by Interface Builder */


#import "LockMgr.h"
#import "ScreenLock.h"
#import <appkit/appkit.h>


void idleTimerProc ( DPSTimedEntry teNumber, double now, char *userData )
{
	[ (id) userData idle ] ;
	return ;
}


@implementation LockMgr

- init
{
	static NXDefaultsVector	SentinelDefaults =
							{
								{ "DimTime", "5" },
								{ "AutoLock", "NO" },
								{ "RequirePassword", "YES" },
								{ NULL }
							} ;

	NXRegisterDefaults ( "Sentinel", SentinelDefaults ) ;
	maxIdleTime = atoi ( NXGetDefaultValue ( "Sentinel", "DimTime" ) ) ;
	autoLock = ! strcmp (
			NXGetDefaultValue ( "Sentinel", "AutoLock" ), "YES" ) ;
	requirePassword = ! strcmp (
			NXGetDefaultValue ( "Sentinel", "RequirePassword" ), "YES" ) ;
	lastDimDestiny = 0 ;

	evsDriver = NXOpenEventStatus() ;

	timedEntry = DPSAddTimedEntry ( 60.0, (DPSTimedEntryProc) idleTimerProc,
			(void *) self, NX_MODALRESPTHRESHOLD ) ;

	return [ super init ] ;
}


- free
{
	NXCloseEventStatus(evsDriver) ;
	DPSRemoveTimedEntry ( timedEntry ) ;
	return [ super free ] ;
}


- ok: sender
{
	char				idleString [ 4 ] ;
	char				autoString [ 4 ] ;
	char				pwordString [ 4 ] ;
	NXDefaultsVector	SentinelDefaults =
						{
							{ "DimTime", idleString },
							{ "AutoLock", autoString },
							{ "RequirePassword", pwordString },
							{ NULL }
						} ;

	autoLock = [ autoLockSwitch intValue ] ;
	requirePassword = [ requirePasswordSwitch intValue ] ;
	maxIdleTime = [ idleSlider intValue ] ;

	sprintf ( idleString, "%d", maxIdleTime ) ;
	strcpy ( autoString, autoLock ? "YES" : "NO" ) ;
	strcpy ( pwordString, requirePassword ? "YES" : "NO" ) ;
	NXWriteDefaults ( "Sentinel", SentinelDefaults ) ;

	[ revertButton setEnabled: NO ] ;
	[ okButton setEnabled: NO ] ;
	[ prefsPanel orderOut: self ] ;

	lastDimDestiny = 0 ;

	return self ;
}


- revert: sender
{
	[ autoLockSwitch setIntValue: autoLock ] ;
	[ requirePasswordSwitch setIntValue: requirePassword ] ;
	[ idleSlider setIntValue: maxIdleTime ] ;
	[ idleField setIntValue: maxIdleTime ] ;
	[ revertButton setEnabled: NO ] ;
	[ okButton setEnabled: NO ] ;
	[ prefsPanel makeKeyAndOrderFront: self ] ;

	lastDimDestiny = 0 ;

	return self ;
}

- changeSetting: sender
{
	switch ( [ sender tag ] )
	{
		case 1:
		{
			[ idleField setIntValue:[ idleSlider intValue ] ] ;
			break ;
		}
		
		case 2:
		{
			[ idleSlider setIntValue:[ idleField intValue ] ] ;
			break ;
		}
	}
	[ revertButton setEnabled: YES ] ;
	[ okButton setEnabled: YES ] ;
	return self ;
}


- idle
{
	double	currentDimDestiny ;

	if ( [ screenLock isLocked ] )
	{
		idleTime = 0 ;
		return self ;
	}
	
	currentDimDestiny = NXAutoDimTime ( evsDriver ) ;
	if ( currentDimDestiny < lastDimDestiny )
	{
		idleTime++ ;
	}
	else
	{
		idleTime = 0 ;
	}
	if ( ( autoLock ) && ( idleTime >= maxIdleTime ) )
	{
		[ screenLock lock: self ] ;
	}
	lastDimDestiny = currentDimDestiny ;
	return self ;
}


- (BOOL) requirePassword
{
	return requirePassword ;
}


- (double) brightness
{
	double		theBrightness ;
	
	theBrightness = NXScreenBrightness(evsDriver) ;
	return theBrightness ;
}


- setBrightness: (double) newBrightness
{
	NXSetScreenBrightness(evsDriver, newBrightness) ;
	return self ;
}

@end
